//
//  SEGSanyBusinessCircleMainViewModel.m
//  Pods
//
//  Created by 梁学彰 on 2020/11/24.
//

#import "SEGSanyBusinessCircleMainViewModel.h"
#import "SEGSanyBusinessCircleManager.h"

@implementation SEGSanyBusinessCircleMainViewModel

- (RACSignal *)requestListData {
    @weakify(self);
    RACSignal *signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        
        WeakSelf;
        [subscriber sendNext:@"1"];
        [SEGSanyBusinessCircleManager requestSanyBusinessCircleDataWithFinished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            [subscriber sendNext:@"0"];
            if (!isSuccessed) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:100 userInfo:@{NSLocalizedDescriptionKey:errorMessage}]];
                return;
            }
            
            if (![result isKindOfClass:[NSDictionary class]] || ![result[@"data"] isKindOfClass:[NSArray class]] || [result[@"data"] count] == 0) {
                [subscriber sendError:[NSError errorWithDomain:NSURLErrorDomain code:200 userInfo:@{NSLocalizedDescriptionKey:@""}]];
                return;
            }
            
            NSMutableArray<SEGSanyBusinessCircleCategoryModel *> *temp = [NSMutableArray array];
            for (NSDictionary *subDic in result[@"data"]) {
                SEGSanyBusinessCircleCategoryModel *categoryModel = [SEGSanyBusinessCircleCategoryModel yy_modelWithDictionary:subDic];
                [temp addObject:categoryModel];
            }
            
            weakSelf.bussinessDatas = [temp copy];
            [subscriber sendCompleted];
        }];
        
        
        return nil;
    }];
    return signal;
}
@end

